#! /bin/sh -e

case "$1" in
  -h|--help)
    echo "Usage: docker-enter CONTAINER [COMMAND]"
    exit 0
    ;;
esac

if [ $(id -ru) -ne 0 ]; then
  echo "You have to be root."
  exit 1
fi

if [ $# -eq 0 ]; then
  echo "Usage: docker-enter CONTAINER [COMMAND]"
  exit 1
fi

container=$1; shift
PID=$(docker inspect --format {{.State.Pid}} "$container")

if [ $# -ne 0 ]; then
   nsenter --target $PID --mount --uts --ipc --net --pid -- $*
   exit $?
fi

nsenter --target $PID --mount --uts --ipc --net --pid
exit 0
